package com.jswlw.controller.api.wrapper;

import com.jswlw.common.BaseApiResponse;
import com.jswlw.controller.api.helper.UserHelper;
import com.jswlw.controller.api.req.aftersale.SaveBookInfoRequest;
import com.jswlw.controller.api.req.aftersale.SaveFeedBackRequest;
import com.jswlw.controller.api.req.aftersale.SaveRepairInfoRequest;
import com.jswlw.entity.base.User;
import com.jswlw.entity.biz.*;
import com.jswlw.service.biz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Component
public class AfterSaleWrapper {

    @Autowired
    RepairInfoService repairInfoService;

    @Autowired
    FeedBackService feedBackService;

    @Autowired
    BookInfoService bookInfoService;

    @Autowired
    UserHelper userHelper;

    @Autowired
    DevInfoService devInfoService;
    @Autowired
    DistributorService distributorService;

    public BaseApiResponse saveRepairInfo(SaveRepairInfoRequest request) {
        //新增报修信息
        RepairInfo repairInfo = new RepairInfo();
        repairInfo.setDevId(request.getDevId());
        repairInfo.setSubmitName(request.getUserName());
        repairInfo.setSubmitTel(request.getTel());
        repairInfo.setContent(request.getContent());
        repairInfo.setBookTime(request.getBookTime());

        DevInfo devInfo = devInfoService.find4Id(repairInfo.getDevId());
        if (Objects.nonNull(devInfo)) {
            repairInfo.setDistributorId(devInfo.getDistributorId());
            repairInfo.setDistributorName(devInfo.getDistributorName());
        }
        repairInfo.setStatus(0);
        repairInfo.setCreateTime(new Date());
        repairInfoService.add(repairInfo);

        return BaseApiResponse.of().withResultSuccess();
    }

    public BaseApiResponse saveFeedBack(SaveFeedBackRequest request) {
        //新增报修信息
        FeedBack feedBack = new FeedBack();
        feedBack.setSubmitName(request.getUserName());
        feedBack.setSubmitTel(request.getTel());
        feedBack.setContent(request.getContent());
        feedBack.setDevId(request.getDevId());
        User user = userHelper.findUser();

        DevInfo devInfo = devInfoService.find4Id(request.getDevId());
        feedBack.setDistributorId(devInfo.getDistributorId());
        feedBack.setDistributorName(devInfo.getDistributorName());
        feedBack.setUserId(user.getId());
        feedBack.setStatus(0);
        feedBack.setCreateTime(new Date());
        feedBackService.add(feedBack);

        return BaseApiResponse.of().withResultSuccess();
    }

    public BaseApiResponse saveBookInfo(SaveBookInfoRequest request) {
        BookInfo bookInfo = new BookInfo();
        User user = userHelper.findUser();
        bookInfo.setUserName(request.getUserName());
        bookInfo.setAddress(request.getAddress());
        bookInfo.setDistributorId(request.getDistributorId());
        bookInfo.setDistributorName(distributorService.find4Id(request.getDistributorId()).getName());
        bookInfo.setUserId(user.getId());
        bookInfo.setTel(request.getTel());
        bookInfo.setDevType(request.getDevType());
        bookInfo.setBookTime(request.getBookTime());
        bookInfo.setCreateTime(new Date());
        bookInfo.setStatus(0);
        bookInfoService.add(bookInfo);
        return BaseApiResponse.of().withResultSuccess();
    }
}
